package com.yidiandao.business.ui.main.shoppingcart.choosegift;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseFragment;
import com.yidiandao.business.ui.adapter.ChooseGiftAdapter;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ChooseGiftSingleListItem;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/** 换购商品-View */
public class ChooseGiftFragment extends BaseFragment
        implements ChooseGiftContract.View, AdapterView.OnItemClickListener {

    private ChooseGiftContract.Presenter mPresenter;

    private Toolbar mToolbar;

    private ListView mListView;

    private TextView mTvConfirmChoose;

    private TextView mTvTotalCount;

    private ChooseGiftAdapter mAdapter;

    public static ChooseGiftFragment getInstance(Bundle extras) {
        ChooseGiftFragment fragment = new ChooseGiftFragment();
        fragment.setArguments(extras);
        return fragment;
    }

    @Override
    public void setPresenter(ChooseGiftContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return ChooseGiftFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mListView = (ListView) mRootView.findViewById(R.id.list_view);
        mTvConfirmChoose = (TextView) mRootView.findViewById(R.id.tv_confirm_choose);
        mTvTotalCount = (TextView) mRootView.findViewById(R.id.tv_total_count);

        mAdapter = new ChooseGiftAdapter(mActivity);
        mListView.setAdapter(mAdapter);

        mPresenter.extraBundle(getArguments());

        initListeners();
    }

    private void initListeners() {
        mToolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finishActivity();
                    }
                });

        mListView.setOnItemClickListener(this);

        mTvConfirmChoose.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.onConfirmChoose();
                    }
                });
    }

    @Override
    public void setDisplayListItems(List<DisplayListItem> displayListItems) {
        mAdapter.setData(displayListItems);
    }

    @Override
    public void notifyDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void finishActivity() {
        mActivity.finish();
    }

    @Override
    public void setChooseGiftCount(int giftCount) {
        mTvTotalCount.setText(UIUtils.getString(R.string.confirm_gift_count, giftCount));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choose_gift;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object itemAtPosition = parent.getItemAtPosition(position);
        if (itemAtPosition instanceof ChooseGiftSingleListItem) {
            mPresenter.onItemClick((ChooseGiftSingleListItem) itemAtPosition);
        }
    }
}
